package com.pat.service;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.pat.manager.RegisterManager;
import com.pat.om.Envelope;
import com.pat.om.Mezzage;
import com.pat.om.Register;
import com.pat.utils.GCMUtil;
import com.pat.utils.Result;

@Path("/messages")
public class MessagingService {

	private RegisterManager regMgr = new RegisterManager();

	@GET
	@Path("register")
	@Produces({ MediaType.APPLICATION_JSON })
	public List<Register> get() {
		return regMgr.get();
	}

	@POST
	@Path("/register")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response get(Register register) {
		regMgr.post(register);
		return Response.status(201).entity(register).build();
	}

	@DELETE
	@Path("/unregister/{regId}")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response delete(@PathParam("regId") String regId) {
		int result = regMgr.delete(regId);
		if (result > 0) {
			return Response.status(200).entity(String.valueOf(result)).build();
		} else {
			return Response.status(304).entity(0).build();
		}
	}

	@POST
	@Path("/send")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response post(Envelope envelope) {
		Result result = GCMUtil.send(envelope);
		return Response.status(201).build();
	}
	
	@POST
	@Path("/send2")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response post2(Mezzage mezzage) {
		Result result = GCMUtil.send(mezzage);
		return Response.status(201).build();
	}
}
